package cn.ming.wfchat.app.me.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.CompoundButton;
import android.widget.Switch;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.kit.dialog.CancelTheDealDialog;
import cn.ming.wfchat.app.dialog.PaymentDialog;
import cn.ming.wfchat.app.dialog.TimingDialog;
import cn.ming.wfchat.app.main.SplashActivity;
import cn.ming.wfchat.kit.ChatManagerHolder;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.ming.wfchat.kit.net.OKHttpHelper;
import cn.ming.wfchat.kit.settings.blacklist.BlacklistListActivity;
import cn.ming.wfchat.kit.widget.OptionItemView;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2020/9/7 3:15 PM
 * 隐私安全
 */
public class YsaqActivity extends WfcBaseActivity {

    @BindView(R.id.ll_ys)
    OptionItemView llYs;
    @BindView(R.id.ll_hmd)
    OptionItemView llHmd;
    @BindView(R.id.ll_cancellation)
    OptionItemView llCancellation;
    @BindView(R.id.ll_empty)
    OptionItemView llEmpty;
    @BindView(R.id.sw_timing)
    Switch swTiming;
    private SharedPreferences sp;

    @Override
    protected int contentLayout() {
        return R.layout.activity_ysaq;
    }

    @Override
    protected void afterViews() {
        setTitle("隐私安全");
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        swTiming.setChecked(sp.getBoolean("emptied", false));
        swTiming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (sp.getInt("numberDay", 0) == 0) {
                    ChatManager.showToast("请先选择定时清空的时间");
                    setLlTimingEmpty();
                    swTiming.setChecked(false);
                } else {
                    sp.edit().putBoolean("emptied", b).apply();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.ll_ys)
    void security() {
        Intent intent = new Intent(YsaqActivity.this, SecurityActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_hmd)
    void setLlHmd() {
        Intent intent = new Intent(this, BlacklistListActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_cancellation)
    void setLlCancellation() {
        CancelTheDealDialog.newInstance().setTitle("提示").setContext("销毁账号后，账号数据会全部清除，如使用该手机重新注册登录，则为新账号，确认销毁吗？").
                setOnClickListener(new CancelTheDealDialog.OnClickListener() {
                    @Override
                    public void successful() {
                        validateLockPassword();
                    }

                    @Override
                    public void cancel() {

                    }
                }).show(YsaqActivity.this);

    }

    @OnClick(R.id.ll_empty)
    void setLlEmpty() {
        CancelTheDealDialog.newInstance().setTitle("提示").setContext("一键清空所有聊天记录及聊天列表，清空后不可恢复！").
                setOnClickListener(new CancelTheDealDialog.OnClickListener() {
                    @Override
                    public void successful() {
                        SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
                        ChatManagerHolder.gChatManager.disconnect(true, true);
                        ChatManagerHolder.gChatManager.connect(sp.getString("id", null), sp.getString("token", null));
                        ChatManager.showToast("清空成功");
                    }

                    @Override
                    public void cancel() {

                    }
                }).show(YsaqActivity.this);
    }

    /**
     * 销毁账号
     */
    void validateLockPassword() {
        SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        if (sp.getBoolean("payPasswordEnable", true)) {
            PaymentDialog.newInstance("确认注销", "请输入支付密码").setOnClickListener(new PaymentDialog.OnClickListener() {
                @Override
                public void getPwd(String pwd) {
                    AppService.Instance().destroyMobile(pwd, new AppService.SendCodeCallback() {
                        @Override
                        public void onUiSuccess() {
                            //不要清除session，这样再次登录时能够保留历史记录。如果需要清除掉本地历史记录和服务器信息这里使用true
                            ChatManagerHolder.gChatManager.disconnect(true, true);
                            SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
                            sp.edit().clear().apply();
                            OKHttpHelper.clearCookies();
                            Intent intent = new Intent(YsaqActivity.this, SplashActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();
                        }

                        @Override
                        public void onUiFailure(String msg) {
                            ChatManager.showToast(msg);
                        }
                    });
                }
            }).show(this);
        } else {
            ChatManager.showToast("请先设置支付密码！");
        }
    }

    @OnClick(R.id.ll_timing_empty)
    void setLlTimingEmpty() {
        TimingDialog.newInstance().show(YsaqActivity.this);
    }

}
